/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProdutoDialog.java
 *
 * Created on 09/11/2013, 17:30:57
 */
package Formularios;

import Classes.MateriaPrimaClass;
import Classes.ProdutoClass;
import Classes.ProdutoMateriaPrimaClass;
import DAO.MateriaPrimaDAO;
import DAO.ProdutoDAO;
import DAO.ProdutoMateriaPrimaDAO;
import java.awt.Color;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.charts.type.MeterShapeEnum;

/**
 *
 * @author Carlos Schaefer
 */
public class ProdutoDialogFrm extends javax.swing.JDialog {

    int incluiralterar;
    
   
    
    public void setNovoProduto(ProdutoClass NovoProduto) {
        this.Produto = NovoProduto;
    }
    
    
    /** Creates new form ProdutoDialog */
    public ProdutoDialogFrm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        getContentPane().setBackground(Color.WHITE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        txtMedTempo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblMateriaPrima = new javax.swing.JTable();
        cboxMateriaPrima = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtQuantidade = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel2.setText("Nome");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Manutenção de Produtos");

        txtCodigo.setEnabled(false);

        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel10.setText("Código");

        jLabel3.setText("Média de tempo para produção");

        tblMateriaPrima.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Código", "CódigoProduto", "CódigoMateriaPrima", "Matéria Prima", "Quantidade"
            }
        ));
        tblMateriaPrima.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblMateriaPrimaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblMateriaPrima);
        tblMateriaPrima.getColumnModel().getColumn(0).setMinWidth(0);
        tblMateriaPrima.getColumnModel().getColumn(0).setPreferredWidth(0);
        tblMateriaPrima.getColumnModel().getColumn(0).setMaxWidth(0);
        tblMateriaPrima.getColumnModel().getColumn(1).setMinWidth(0);
        tblMateriaPrima.getColumnModel().getColumn(1).setPreferredWidth(0);
        tblMateriaPrima.getColumnModel().getColumn(1).setMaxWidth(0);
        tblMateriaPrima.getColumnModel().getColumn(2).setMinWidth(0);
        tblMateriaPrima.getColumnModel().getColumn(2).setPreferredWidth(0);
        tblMateriaPrima.getColumnModel().getColumn(2).setMaxWidth(0);
        tblMateriaPrima.getColumnModel().getColumn(4).setMinWidth(70);
        tblMateriaPrima.getColumnModel().getColumn(4).setPreferredWidth(70);
        tblMateriaPrima.getColumnModel().getColumn(4).setMaxWidth(70);

        cboxMateriaPrima.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Matéria Prima");

        jLabel5.setText("Quantidade");

        jButton3.setText("Excluir Mátéria Prima");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Inserir");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel10)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2)
                            .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(cboxMateriaPrima, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel3)
                            .addComponent(txtMedTempo, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(11, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(297, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton3)
                .addContainerGap(332, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtMedTempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboxMateriaPrima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addGap(72, 72, 72)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(77, 77, 77))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    
    if (Produto==null){
        Produto.setDescricao(txtNome.getText());
        Produto.setMediatempoproducao(Double.parseDouble(txtMedTempo.getText()));
        
        dao.insert(Produto);
        
   }else{
       Produto.setDescricao(txtNome.getText());
       Produto.setMediatempoproducao(Double.parseDouble(txtMedTempo.getText()));
       dao.update(Produto);
       
   }   
    
    this.dispose();
   
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
dispose();
}//GEN-LAST:event_jButton2MouseClicked

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
//        this.setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    montarCombo();
    
    if (Produto==null){
        incluiralterar = 1;//incluindo
    }else{
        incluiralterar = 2;//alterando
        txtCodigo.setText(""+Produto.getId());
        txtNome.setText(Produto.getDescricao());
        txtMedTempo.setText(""+Produto.getMediatempoproducao());
        
        ListarMateriasPrimas();
        
    }
    
    
    


        
}//GEN-LAST:event_formWindowOpened

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

   if (Produto==null){
        Produto.setDescricao(txtNome.getText());
        Produto.setMediatempoproducao(Double.parseDouble(txtMedTempo.getText()));
        
        dao.insert(Produto);
        
   }else{
       Produto.setDescricao(txtNome.getText());
       Produto.setMediatempoproducao(Double.parseDouble(txtMedTempo.getText()));
       dao.update(Produto);
       
   }   
        
        
        
    
    
    materia = new ProdutoMateriaPrimaClass();
    materia.setProduto(Produto);
    materia.setMateriaprima( (MateriaPrimaClass) cboxMateriaPrima.getSelectedItem());
    materia.setQuantidade(Double.parseDouble(txtQuantidade.getText()));
    
    matdao.insert(materia);
    ListarMateriasPrimas();
    
    
    
    
}//GEN-LAST:event_jButton4ActionPerformed

private void tblMateriaPrimaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblMateriaPrimaMouseClicked

     int linha = tblMateriaPrima.getSelectedRow();
     materia = matdao.findbyid(Integer.parseInt(tblMateriaPrima.getModel().getValueAt(linha, 0).toString()));
    
    
    
    
    
}//GEN-LAST:event_tblMateriaPrimaMouseClicked

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    matdao.delete(materia);
    ListarMateriasPrimas();
}//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ProdutoDialogFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProdutoDialogFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProdutoDialogFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProdutoDialogFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ProdutoDialogFrm dialog = new ProdutoDialogFrm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cboxMateriaPrima;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblMateriaPrima;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtMedTempo;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtQuantidade;
    // End of variables declaration//GEN-END:variables
    private MateriaPrimaDAO cdao = new MateriaPrimaDAO();
    private ProdutoMateriaPrimaDAO matdao = new ProdutoMateriaPrimaDAO();
    private ProdutoMateriaPrimaClass materia = new ProdutoMateriaPrimaClass();
    private ProdutoClass Produto = new ProdutoClass();
    private ProdutoDAO dao = new ProdutoDAO();
    
    
    private void montarCombo() {
        List<MateriaPrimaClass> lista = cdao.findall();
        
        DefaultComboBoxModel cbox = new DefaultComboBoxModel( new Vector(lista));
        cboxMateriaPrima.setModel(cbox); 
        
                
        
    }

    private void ListarMateriasPrimas() {
        
        DefaultTableModel model  = (DefaultTableModel) tblMateriaPrima.getModel();
        model.setNumRows(0);        

        for (ProdutoMateriaPrimaClass materias : cdao.findallCodProduto(Produto.getId())) {
            model.addRow(new String[]{
                        "" + materias.getId(),
                        "" + materias.getProduto().getId(),
                        "" + materias.getMateriaprima().getId(),
                        "" + materias.getMateriaprima().getDescricao(),
                        "" + materias.getQuantidade()
                    });
            
        materia=materias;    
        
        }
        
        
        
        
    }
}
